package com.ruoyi.oa.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.oa.domain.OaFinanceTripApply;
import com.ruoyi.oa.domain.OaProject;
import com.ruoyi.oa.domain.bo.OaFinanceTripApplyBo;
import com.ruoyi.oa.domain.vo.OaFinanceTripApplyVo;
import com.ruoyi.oa.domain.vo.OaPurchaseApplyVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.service.IOaFinanceTripApplyService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出差申请
 *
 * @author zhoul
 * @date 2023-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/financeTripApply")
public class OaFinanceTripApplyController extends BaseController {

    private final IOaFinanceTripApplyService iOaFinanceTripApplyService;

    /**
     * 查询出差申请列表
     */
    @SaCheckPermission("oa:financeTripApply:list")
    @GetMapping("/list")
    public TableDataInfo<OaFinanceTripApplyVo> list(OaFinanceTripApplyBo bo, PageQuery pageQuery) {
        return iOaFinanceTripApplyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出出差申请列表
     */
    @SaCheckPermission("oa:financeTripApply:export")
    @Log(title = "出差申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaFinanceTripApplyBo bo, HttpServletResponse response) {
        List<OaFinanceTripApplyVo> list = iOaFinanceTripApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "出差申请", OaFinanceTripApplyVo.class, response);
    }


    /**
     * 获取出差申请详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("oa:financeTripApply:query")
    @GetMapping("/{id}")
    public R<OaFinanceTripApplyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaFinanceTripApplyService.queryById(id));
    }

    /**
     * 新增出差申请
     */
    @SaCheckPermission("oa:financeTripApply:add")
    @Log(title = "出差申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaFinanceTripApplyBo> add(@Validated(AddGroup.class) @RequestBody OaFinanceTripApplyBo bo) {
        if (StringUtils.isNotBlank(bo.getProjectName())) {
            OaFinanceTripApplyVo oaFinanceTripApplyVo = iOaFinanceTripApplyService.queryByProjectName(bo.getProjectName());
            if (ObjectUtil.isNotEmpty(oaFinanceTripApplyVo)) {
                return R.fail(bo.getProjectName() + "已经存在,不能重复申请.");
            }
        }
        return R.ok(iOaFinanceTripApplyService.insertByBo(bo));
    }

    /**
     * 修改出差申请
     */
    @SaCheckPermission("oa:financeTripApply:edit")
    @Log(title = "出差申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaFinanceTripApply> edit(@Validated(EditGroup.class) @RequestBody OaFinanceTripApplyBo bo) {
        return R.ok(iOaFinanceTripApplyService.updateByBo(bo));
    }

    /**
     * 删除出差申请
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oa:financeTripApply:remove")
    @Log(title = "出差申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaFinanceTripApplyService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    @RequestMapping("/queryByProjectName")
    public R<OaFinanceTripApplyVo> queryByProjectName(@RequestBody Map<String,Object> map) {
        return R.ok(iOaFinanceTripApplyService.queryByProjectName(map.get("projectName").toString()));
    }


    @Log(title = "获取没有出差申请的项目信息")
    @GetMapping("/getProjectList")
    public R<List<OaProject>> getProjectList() {
        return R.ok(iOaFinanceTripApplyService.getProjectList());
    }

    /**
     * 查看某人本月的出差次数
     * @param userId
     * @param month
     * @return
     */
    @GetMapping("/getUserTripByMonth")
    public R<Integer> getUserTripByMonth(@RequestParam(value = "userId", required = false) String userId,
                                            @RequestParam(value = "month", required = false) String month){
        return R.ok(iOaFinanceTripApplyService.getUserTripByMonth(month,userId));
    }
}
